package com.zhaolang.qogir.ui.settings;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.suke.widget.SwitchButton;
import com.umeng.socialize.utils.Log;
import com.zhaolang.qogir.R;
import com.zhaolang.qogir.ui.BaseActivity;
import com.zhaolang.qogir.ui.TitleView;
import com.zhaolang.qogir.util.HttpConstant;
import com.zhaolang.qogir.util.Slog;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ActivityOrderManage extends BaseActivity implements View.OnClickListener, SwitchButton.OnCheckedChangeListener {

    private SwitchButton sb;
    private OkHttpClient okHttpClient = new OkHttpClient();
    private boolean slideStatus = false;
    private TitleView mTitle;
    private TextView tvStatus;
    private static final int BASE = 0x500;
    private static final int MSG_SHOP_REQUEST = BASE + 1;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_SHOP_REQUEST:
                    int status = (int) msg.obj;
                    if (status == 0) {
                        slideStatus = false;
                        tvStatus.setText(R.string.ui_string_settings_no_sale);
                        tvStatus.setTextColor(getResources().getColor(R.color.red));
                        sb.setChecked(false);
                    } else if (status == 1) {
                        slideStatus = true;
                        tvStatus.setText(R.string.ui_string_settings_sale);
                        tvStatus.setTextColor(getResources().getColor(R.color.black));
                        sb.setChecked(true);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_order_manage);
        findView();
        requestShopStatus();
    }

    private void findView() {
        View title = findViewById(R.id.vg_title);
        mTitle = new TitleView(this, title);
        mTitle.setCenterText(R.string.settings_printer, null);
        mTitle.setLeftIcon(R.drawable.btn_back, this);
        tvStatus = findViewById(R.id.tv_settings_sale_auto);
        sb = findViewById(R.id.btn_shop_manage);
        sb.setOnClickListener(this);
        sb.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case TitleView.ID_LEFT:
                finish();
                break;
            default:
                break;
        }
    }

    private void requestShopStatus() {
        String url = HttpConstant.getShopStatus(null) + "?SHOP_ID=15651723391";
        final Request request = new Request.Builder().url(url).build();
        if (okHttpClient != null) {
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Slog.e("onFailure: " + e.getMessage());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String body = response.body().string();
                    String headers = response.headers().toString();
                    parseShopString(body);
                    Slog.i("onResponse: " + body);
                }
            });
        }
    }

    private void parseShopString(String body) {
        try {
            JSONObject jsonObject = new JSONObject(body);
            if (jsonObject.optString("status").equals("success")) {
                String classValues = jsonObject.optString("shopContent");
                JSONObject obj = new JSONObject(classValues);
                int status = obj.optInt("status");
                Message message = new Message();
                message.what = MSG_SHOP_REQUEST;
                message.obj = status;
                handler.sendMessage(message);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void updateShopStatus(int id) {
        String url = HttpConstant.setShopStatus(null) + "?SHOP_ID=15651723391&STATUS=" + id;
        final Request request = new Request.Builder().url(url).build();
        if (okHttpClient != null) {
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Slog.e("onFailure: " + e.getMessage());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String body = response.body().string();
                    String headers = response.headers().toString();
                    Slog.i("onResponse: " + body);
                }
            });
        }
    }

    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        if (view.isChecked()) {
            tvStatus.setText(R.string.ui_string_settings_sale);
            tvStatus.setTextColor(getResources().getColor(R.color.black));
            updateShopStatus(1);
        } else {
            tvStatus.setText(R.string.ui_string_settings_no_sale);
            tvStatus.setTextColor(getResources().getColor(R.color.red));
            updateShopStatus(0);
        }
    }
}
